/**
 * Created by WangJunjie on 16/07/11.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'wechatred',
                model: {
                    url: _global.url.api + 'wechatred',
                    event: {
                        '.item-remove:click': 'remove',
                        '.item-edit:click': 'edit',
                        '.item-stop:click': 'stop',
                        '.item-start:click': 'start'
                    },

                    stop: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'wechatred?status=0&id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.status = 0;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },

                    start: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'wechatred?status=1&id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.status = 1;
                                    _this.create_element();
                                } else {
                                    Alert({msg: '启用活动前请前往活动管理设置奖品', type: 'error'});
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },

                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?记得到活动列表删除活动关联抽奖ID',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        dialog({
                            title: '编辑活动',
                            content: template('edit-tpl', _this.data),
                            width: 700,
                            quickClose: true,
                            ok: function () {
                                _this.data.name = $("input[name='title']").val();
                                if (_this.data.name == '' || _this.data.name == undefined) {
                                    Alert({msg: '请输入活动标题', type: 'error'});
                                    return false;
                                }

                                var imgs = new Array();
                                var flag = false;
                                $("input[name='imgs']").each(function () {
                                    if ($(this).val() != '') {
                                        imgs.push($(this).val());
                                        flag = true;
                                    } else {
                                        flag = false;
                                        return false;
                                    }
                                });
                                _this.data.imgs = imgs.join(',');
                                if (!flag || imgs.length < 4) {
                                    Alert({msg: '请上传图片', type: 'error'});
                                    return false;
                                }

                                _this.data.reward_count = $("input[name='reward_count']").val();

                                if (new Model({
                                        url: _global.url.api + 'wechatred/edit',
                                        data: _this.data
                                    }).put()) {
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {

            var _this = this;
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加活动',
                    content: template('add-tpl', {}),
                    width: 700,
                    ok: function () {

                        _this.data.name = $("input[name='title']").val();
                        if (_this.data.name == '' || _this.data.name == undefined) {
                            Alert({msg: '请输入活动标题', type: 'error'});
                            return false;
                        }

                        var imgs = new Array();
                        var flag = false;
                        $("input[name='imgs']").each(function () {
                            if ($(this).val() != '') {
                                imgs.push($(this).val());
                                flag = true;
                            } else {
                                flag = false;
                                return false;
                            }
                        });
                        _this.data.imgs = imgs.join(',');
                        if (!flag || imgs.length < 4) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }

                        //_this.data.reward_count = $("input[name='reward_count']").val();


                        console.log(_this.data);
                        if (new Model({
                                url: _global.url.api + 'wechatred',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();

            });


            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreviews(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });

        },
        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
        imgPreviews: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view' + img.item).html(template('img-tpl', img));
            }
        },


    };

    new index();
});